package com.kakarote.hrm.controller;

import cn.hutool.core.collection.ListUtil;
import com.alibaba.fastjson.JSONObject;
import com.kakarote.common.log.annotation.OperateLog;
import com.kakarote.common.log.entity.OperationLog;
import com.kakarote.common.log.entity.OperationResult;
import com.kakarote.common.log.enums.ApplyEnum;
import com.kakarote.common.log.enums.BehaviorEnum;
import com.kakarote.common.log.enums.OperateObjectEnum;
import com.kakarote.common.log.enums.OperateTypeEnum;
import com.kakarote.core.common.Result;
import com.kakarote.core.entity.BasePage;
import com.kakarote.core.entity.UploadEntity;
import com.kakarote.hrm.entity.BO.QueryRecruitUserPageListBO;
import com.kakarote.hrm.entity.PO.HrmRecruitUser;
import com.kakarote.hrm.entity.VO.RecruitUserVO;
import com.kakarote.hrm.service.IHrmRecruitUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 招聘人员 前端控制器
 * </p>
 *
 * @author system
 * @since 2024-01-01
 */
@RestController
@RequestMapping("/hrmRecruitUser")
@Api(tags = "招聘管理-招聘人员")
public class HrmRecruitUserController {

    @Autowired
    private IHrmRecruitUserService recruitUserService;

    @ApiOperation("添加招聘人员")
    @PostMapping("/addRecruitUser")
    @OperateLog(apply = ApplyEnum.HRM, object = OperateObjectEnum.HRM_RECRUIT_USER, behavior = BehaviorEnum.SAVE)
    public Result addRecruitUser(@Validated @RequestBody HrmRecruitUser recruitUser) {
        OperationLog operationLog = recruitUserService.addRecruitUser(recruitUser);
        return OperationResult.ok(operationLog);
    }

    @ApiOperation("修改招聘人员")
    @PostMapping("/setRecruitUser")
    @OperateLog(apply = ApplyEnum.HRM, object = OperateObjectEnum.HRM_RECRUIT_USER, behavior = BehaviorEnum.UPDATE)
    public Result setRecruitUser(@Validated @RequestBody HrmRecruitUser recruitUser) {
        OperationLog operationLog = recruitUserService.setRecruitUser(recruitUser);
        return OperationResult.ok(operationLog);
    }

    /**
     * 获取招聘人员详情
     *
     * @param recruitUserId 招聘人员id
     */
    @ApiOperation("获取招聘人员详情")
    @PostMapping("/queryById/{recruitUserId}")
    public Result<RecruitUserVO> queryById(@PathVariable("recruitUserId") Long recruitUserId) {
        RecruitUserVO recruitUserVO = recruitUserService.queryById(recruitUserId);
        return Result.ok(recruitUserVO);
    }

    @ApiOperation("查询招聘人员列表")
    @PostMapping("/queryRecruitUserPageList")
    public Result<BasePage<RecruitUserVO>> queryRecruitUserPageList(@RequestBody QueryRecruitUserPageListBO queryRecruitUserPageListBO) {
        BasePage<RecruitUserVO> page = recruitUserService.queryRecruitUserPageList(queryRecruitUserPageListBO);
        return Result.ok(page);
    }

    @ApiOperation("根据计划id查询所有招聘人员列表")
    @PostMapping("/queryByPlanId/{planId}")
    public Result<List<RecruitUserVO>> queryByPlanId(@PathVariable("planId") Long planId) {
        List<RecruitUserVO> userList = recruitUserService.queryByPlanId(planId);
        return Result.ok(userList);
    }

    @ApiOperation("根据记录id查询所有招聘人员列表")
    @PostMapping("/queryByRecordId/{recordId}")
    public Result<List<RecruitUserVO>> queryByRecordId(@PathVariable("recordId") Long recordId) {
        List<RecruitUserVO> userList = recruitUserService.queryByRecordId(recordId);
        return Result.ok(userList);
    }

    @ApiOperation("根据过程id查询所有招聘人员列表")
    @PostMapping("/queryByProcesId/{procesId}")
    public Result<List<RecruitUserVO>> queryByProcesId(@PathVariable("procesId") Long procesId) {
        List<RecruitUserVO> userList = recruitUserService.queryByProcesId(procesId);
        return Result.ok(userList);
    }

    @ApiOperation("删除招聘人员")
    @PostMapping("/deleteRecruitUser/{recruitUserId}")
    @OperateLog(apply = ApplyEnum.HRM, object = OperateObjectEnum.HRM_RECRUIT_USER, behavior = BehaviorEnum.DELETE)
    public Result deleteRecruitUser(@PathVariable("recruitUserId") Long recruitUserId) {
        OperationLog operationLog = recruitUserService.deleteRecruitUser(recruitUserId);
        return OperationResult.ok(operationLog);
    }

    @ApiOperation("批量删除招聘人员")
    @PostMapping("/batchDeleteRecruitUser")
    @OperateLog(apply = ApplyEnum.HRM, object = OperateObjectEnum.HRM_RECRUIT_USER, behavior = BehaviorEnum.DELETE)
    public Result batchDeleteRecruitUser(@RequestBody List<Long> recruitUserIds) {
        OperationLog operationLog = recruitUserService.batchDeleteRecruitUser(recruitUserIds);
        return OperationResult.ok(operationLog);
    }

    @ApiOperation("根据计划id删除所有招聘人员")
    @PostMapping("/deleteByPlanId/{planId}")
    @OperateLog(apply = ApplyEnum.HRM, object = OperateObjectEnum.HRM_RECRUIT_USER, behavior = BehaviorEnum.DELETE)
    public Result deleteByPlanId(@PathVariable("planId") Long planId) {
        OperationLog operationLog = recruitUserService.deleteByPlanId(planId);
        return OperationResult.ok(operationLog);
    }

    @ApiOperation("根据记录id删除所有招聘人员")
    @PostMapping("/deleteByRecordId/{recordId}")
    @OperateLog(apply = ApplyEnum.HRM, object = OperateObjectEnum.HRM_RECRUIT_USER, behavior = BehaviorEnum.DELETE)
    public Result deleteByRecordId(@PathVariable("recordId") Long recordId) {
        OperationLog operationLog = recruitUserService.deleteByRecordId(recordId);
        return OperationResult.ok(operationLog);
    }

    @ApiOperation("根据过程id删除所有招聘人员")
    @PostMapping("/deleteByProcesId/{procesId}")
    @OperateLog(apply = ApplyEnum.HRM, object = OperateObjectEnum.HRM_RECRUIT_USER, behavior = BehaviorEnum.DELETE)
    public Result deleteByProcesId(@PathVariable("procesId") Long procesId) {
        OperationLog operationLog = recruitUserService.deleteByProcesId(procesId);
        return OperationResult.ok(operationLog);
    }

    @PostMapping("/exportRecruitUser")
    @ApiOperation("导出招聘人员")
    @OperateLog(apply = ApplyEnum.HRM, object = OperateObjectEnum.HRM_RECRUIT_USER, behavior = BehaviorEnum.EXCEL_EXPORT, type = OperateTypeEnum.EXPORT)
    public void exportRecruitUser(@RequestBody QueryRecruitUserPageListBO queryRecruitUserPageListBO, HttpServletResponse response) throws IOException {
        recruitUserService.exportRecruitUser(response, queryRecruitUserPageListBO);
    }

    @PostMapping("/downloadExcel")
    @ApiOperation("获取导入模板")
    public void downloadExcel(HttpServletResponse response) throws IOException {
        recruitUserService.downloadExcel(response);
    }

    @PostMapping("/excelImport")
    @ApiOperation("excel导入招聘人员")
    @OperateLog(apply = ApplyEnum.HRM, behavior = BehaviorEnum.EXCEL_IMPORT, type = OperateTypeEnum.IMPORT, object = OperateObjectEnum.HRM_RECRUIT_USER)
    public Result<JSONObject> excelImport(@RequestParam("file") MultipartFile file) {
        JSONObject object = recruitUserService.excelImport(file);

        UploadEntity uploadEntity = recruitUserService.uploadTempFile(file, null);

        OperationLog operationLog = new OperationLog();
        operationLog.setOperationObject("uploadData", uploadEntity.getFileId());
        operationLog.setOperationObject("name", "招聘人员");

        JSONObject info = new JSONObject();
        info.put("count", object.getInteger("totalSize"));
        info.put("errorSize", object.getInteger("errSize"));
        info.put("errorFile", object.getString("token"));
        operationLog.setOperationInfo(info.toJSONString());
        return OperationResult.ok(object, ListUtil.toList(operationLog));
    }
}

